package org.batatasframework.chart.columns;

import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.batatasframework.chart.ChartCell;
import org.batatasframework.chart.data.ChartInitializer;

/**
 * 二维的head，即可生成柱图或线图等
 * @author jackson
 *
 */
public class ColumnAxis2Dimension implements ChartColumnAxis{

	private final ChartColumnAxis x;
	private final ChartColumnAxis y;
	
	public ColumnAxis2Dimension(ChartColumnAxis x, ChartColumnAxis y){
		this.x = x;
		this.y = y;
	}

	public ColumnAxis2Dimension(ChartColumnAxis x, ColumnAxis2Dimension source){
		this.x = x;
		this.y = source.y;
	}
	
	public ChartColumnAxis axisX(){ return x; }
	public ChartColumnAxis axisY(){ return y; }
	
	@Override
	public int size() {
		return x.size() + y.size();
	}

	@Override
	public int rowspan() {
		return Math.max(x.rowspan(), y.rowspan());
	}

	@Override
	public ChartCell newCell() {
		return new ChartCellTwoDimension(x, y);
	}
	
	private class ChartCellTwoDimension implements ChartCell{
		private final ChartCell x, y;
		
		private ChartCellTwoDimension(ChartColumnAxis x, ChartColumnAxis y){
			this.x = x.newCell();
			this.y = y.newCell();
		}
		
		@Override
		public void read(Object[] row) {
			x.read(row);
			y.read(row);
		}

		@Override
		public void write(List<Object> item) {
			x.write(item);
			y.write(item);
		}

		@Override
		public void add(ChartCell augend) {
			if(augend instanceof ChartCellTwoDimension){
				ChartCellTwoDimension z = (ChartCellTwoDimension)augend;
				x.add(z.x);
				y.add(z.y);
			}else{
				x.add(augend);
				y.add(augend);
			}
		}
		
		@Override
		public String toString() {
			return x.toString() + "-" + y.toString();
		}
	}
	
	@Override
	public void resolveHead(String[] heads, int deep) {
		x.resolveHead(heads, deep);
		y.resolveHead(heads, deep);
	}

	@Override
	public void resolveHead(XSSFRow[] heads, XSSFCellStyle style, int deep) {
		x.resolveHead(heads, style, deep);
		y.resolveHead(heads, style, deep);
	}

	@Override
	public String toString() {
		return x.toString() + "-" + y.toString();
	}

	@Override
	public ChartColumn initialize(ChartInitializer initializer) {
		x.initialize(initializer);
		y.initialize(initializer);
		return this;
	}

	@Override
	public int getOrdinal() {
		return x.getOrdinal();
	}
}
